using System.ComponentModel.Composition;
using MediaOwl.Common.Dialog;

namespace MediaOwl.Common.ViewModels
{
    [Export, PartCreationPolicy(CreationPolicy.NonShared)]
    public class DialogMessageViewModel : DialogScreen
    {
        #region Properties with Backing-Fields

        private DialogType dialogType;
        public DialogType DialogType
        {
            get { return dialogType; }
            set
            {
                dialogType = value;
                NotifyOfPropertyChange(() => DialogType);

                ModifyDialog();
            }
        }

        private bool hasOk;
        public bool HasOk
        {
            get { return hasOk; }
            set
            {
                hasOk = value;
                NotifyOfPropertyChange(() => HasOk);
            }
        }

        private bool hasCancel;
        public bool HasCancel
        {
            get { return hasCancel; }
            set
            {
                hasCancel = value;
                NotifyOfPropertyChange(() => HasCancel);
            }
        }

        private bool hasYes;
        public bool HasYes
        {
            get { return hasYes; }
            set
            {
                hasYes = value;
                NotifyOfPropertyChange(() => HasYes);
            }
        }

        private bool hasNo;
        public bool HasNo
        {
            get { return hasNo; }
            set
            {
                hasNo = value;
                NotifyOfPropertyChange(() => HasNo);
            }
        }

        private bool hasIgnore;
        public bool HasIgnore
        {
            get { return hasIgnore; }
            set
            {
                hasIgnore = value;
                NotifyOfPropertyChange(() => HasIgnore);
            }
        }

        private bool hasRetry;
        public bool HasRetry
        {
            get { return hasRetry; }
            set
            {
                hasRetry = value;
                NotifyOfPropertyChange(() => HasRetry);
            }
        }

        #endregion

        #region Methods

        public void WithData(string message, string title = null, DialogType type = DialogType.OkOnly)
        {
            DisplayName = string.IsNullOrEmpty(title)
                ? "Dialog"
                : title;

            DialogMessage = message;

            DialogType = type;
        }

        private void ModifyDialog()
        {
            HasCancel = false;
            HasIgnore = false;
            HasNo = false;
            HasOk = false;
            HasRetry = false;
            HasYes = false;
            DialogMessageResult = DialogResult.None;

            switch (DialogType)
            {
                case DialogType.IgnoreRetry:
                    HasIgnore = HasRetry = true;
                    break;
                case DialogType.OkCancel:
                    HasOk = HasCancel = true;
                    break;
                case DialogType.OkOnly:
                    HasOk = true;
                    break;
                case DialogType.RetryCancel:
                    HasRetry = HasCancel = true;
                    break;
                case DialogType.YesNo:
                    HasYes = HasNo = true;
                    break;
                case DialogType.YesNoCancel:
                    HasYes = HasNo = HasCancel = true;
                    break;
            }
        }

        public void Ok()
        {
            DialogMessageResult = DialogResult.Ok;
            TryClose();
        }

        public void Cancel()
        {
            DialogMessageResult = DialogResult.Cancel;
            TryClose();
        }

        public void Yes()
        {
            DialogMessageResult = DialogResult.Yes;
            TryClose();
        }

        public void No()
        {
            DialogMessageResult = DialogResult.No;
            TryClose();
        }

        public void Retry()
        {
            DialogMessageResult = DialogResult.Retry;
            TryClose();
        }

        public void Ignore()
        {
            DialogMessageResult = DialogResult.Ignore;
            TryClose();
        }

        #endregion
    }
}